package com.qf.team2.netdisc.job;

import com.qf.team2.netdisc.common.pojo.entity.CancelOrder;
import com.qf.team2.netdisc.service.CancelService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author QWER
 */
public class PrivateJob implements Job {

    @Autowired
    private CancelService cancelService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        List<CancelOrder> cancelOrders = cancelService.selectOrder();
        for(CancelOrder cancelOrder:cancelOrders){
            String now = sdf.format(new Date());
            String createTime = sdf.format(cancelOrder.getCreateTime());

            long nowL = 0;
            long creatTimeL = 0;
            try {
                nowL = sdf.parse(now).getTime();
                creatTimeL = sdf.parse(createTime).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //判断时间间隔是否符合条件，符合则修改分享权限
            if (nowL - creatTimeL >= 1000*60*60*24*30L) {
                cancelService.cancelVip(cancelOrder.getId(),cancelOrder.getUserId());
            }

            //测试5分钟的时效
            if (nowL - creatTimeL >= 3000*10L) {
                System.out.println("开始执行");
                cancelService.cancelVip(cancelOrder.getId(),cancelOrder.getUserId());
            }
        }
    }
}
